package sibu.cn.mydemo.camera;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import sibu.cn.mydemo.R;

public class CameraActivity extends AppCompatActivity {

    private static int REQ_1= 1;
    private static int REQ_2= 2;
    private String filepath = "";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        filepath = Environment.getExternalStorageDirectory().getPath();
        filepath = filepath+"/tmp.png";
    }

    public void  startCamera(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri phUri = Uri.fromFile(new File(filepath));
        intent.putExtra(MediaStore.EXTRA_OUTPUT,phUri);//如此便可修改图片的存放路径
        startActivityForResult(intent, REQ_2);
    }

    public void  startCustomCamera(View view){
        Intent intent = new Intent(this,CustomCameraActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

      if(resultCode == RESULT_OK){
          if(requestCode==REQ_1){

              Bundle bundle =   data.getExtras();
              Bitmap bitmap = (Bitmap) bundle.get("data");//此时拿到的只是相机拍照回来的缩略图
          }

          if(requestCode ==REQ_2){
              try {
                  FileInputStream fileInputStream = new FileInputStream(filepath);
                  Bitmap bitmap = BitmapFactory.decodeStream(fileInputStream);
                  //如此就可以拿到拍照后的原图了
              } catch (FileNotFoundException e) {
                  e.printStackTrace();
              }

          }
      }

    }
}
